﻿///***************************************************************
/// 시  스  템 : Updms.Bby
/// 단위시스템 : BBY
/// 계      층 : Presentation Layer
/// 프로그램명 : Bby2012r
/// 파  일  명 : Bby2012r.aspx.cs
/// 설      명 : 연간식단표
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-10-10 송시명   최초 작성
/// CH001  2008-11-26 송시명   월별식단표 추가
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Bby.Exe;


namespace Updms.Bby
{
	public partial class Bby2012r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Bby2012r";
		public static readonly string gs_path_info = "/updms/Bby/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Etc etc = new CommCode.Etc(LANG_SET);

		//검색
		protected string kw1 = "";	// 조회년월
		protected string kw2 = "";	// 주재료

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetLayout();
			SetBaseYear();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//onKeyPress이벤트 (엔터검색)
			txt_search_baby_food_stuff.Attributes.Add("onkeypress", "enterSearch(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_base_year.SelectedValue = kw1.Substring(0, 4).ToString();
						ddl_search_base_month.SelectedValue = kw1.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = txt_search_baby_food_stuff.Text.Trim();
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_baby_food_stuff.Text = kw2;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					kw2 = txt_search_baby_food_stuff.Text.Trim();
					break;
			}
		}

		#endregion

		#region 마스터 Method (SetLayout, SetBaseYear)

		private void SetLayout()
		{
			etc.SetLayout(rbl_layout);
		}

		//기준년도 설정
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();
			ddl_search_base_year.Items.Add(new ListItem("2008", "2008"));
			
			// 2007 ~ 2008
			//for (int i = 2007; i <= 2008; i++)
			//{
			//	string ls_year = i.ToString();
			//	ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			//}			
			//ddl_search_base_year.SelectedValue = "2008";	//기본값은 2008
			ddl_search_base_month.SelectedValue = "03";//DateUtil.GetMonthEx(0, "MM");
		}

		#endregion

		#region 디테일 월별 (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
			string ls_calc_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM");

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_ym.Substring(0, 4).ToString()))
			{
				ddl_search_base_year.SelectedValue = ls_calc_ym.Substring(0, 4).ToString();
				ddl_search_base_month.SelectedValue = ls_calc_ym.Substring(5, 2).ToString();
			}

			//버튼처리
			SetEnabledMonthBtn();
		}

		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled
				= (ddl_search_base_year.SelectedIndex == 0 && ddl_search_base_month.SelectedIndex == 0) ? false : true;

			ibtn_next_month.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1 && ddl_search_base_month.SelectedIndex == 11) ? false : true;
		}

		//당월설정
		private void SetThisMonth()
		{
			string ls_this_ym = DateUtil.GetNow("yyyy-MM");
			string ls_this_year = ls_this_ym.Substring(0, 4).ToString();
			string ls_this_month = ls_this_ym.Substring(5, 2).ToString();
			
			//현재년이 있으면 바인딩
			if(WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_this_year))
			{
				ddl_search_base_year.SelectedValue = ls_this_year;
				ddl_search_base_month.SelectedValue = ls_this_month;
			}
			else
			{
				CallMsgBoxInAjax("MSG_ERR_150");	//현재월의 데이터가 없습니다
			}
		}

		#endregion

		#region 디테일 년도별 (SetPrevNextYear, SetEnabledYearBtn)

		//이전년, 다음년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			int li_calc_year = Convert.ToInt32(ddl_search_base_year.SelectedValue) + pi_year_cnt;	//2008 + (-1)

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, li_calc_year.ToString()))
			{
				ddl_search_base_year.SelectedValue = li_calc_year.ToString();
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//이전년, 다음년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled
				= (ddl_search_base_year.SelectedIndex == 0) ? false : true;

			ibtn_next_year.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");

			//현재년이 있으면 바인딩
			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_this_year))
			{
				ddl_search_base_year.SelectedValue = ls_this_year;
			}
			else
			{
				CallMsgBoxInAjax("MSG_ERR_149");	//현재년도의 데이터가 없습니다
			}
		}

		#endregion

		#region 리스트 (SelectMonthly, SelectYearly, SearchProcess)

		//월간
		private void SelectMonthly()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_baby_food_stuff", typeof(string), kw2);

			using (Bby2012r_NT _Bby2012r_NT = new Bby2012r_NT())
			{
				lo_ds.Merge(_Bby2012r_NT.GetMonthlyBabyFoodStuff(lo_dp));
			}

			gv_monthly.DataSource = lo_ds;
			gv_monthly.DataBind();

			//결과출력(키워드, 검색행수)
			lbl_this_month.Text = DateUtil.ConvertDateMask(kw1, "yyyy-MM", LANG_SET);
		}

		//연간
		private void SelectYearly()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_baby_food_stuff", typeof(string), kw2);

			using (Bby2012r_NT _Bby2012r_NT = new Bby2012r_NT())
			{
				lo_ds.Merge(_Bby2012r_NT.GetYearlyBabyFoodStuff(lo_dp));
			}

			gv_yearly.DataSource = lo_ds;
			gv_yearly.DataBind();

			//결과출력(키워드, 검색행수)
			lbl_this_year.Text = DateUtil.GetCalcMonth(kw1, 0, "yyyy");
		}

		//검색
		private void SearchProcess()
		{	
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			switch (rbl_layout.SelectedValue)
			{
				case "AH001":	//월별
					table_monthly.Visible = true;
					table_yearly.Visible = false;
					SelectMonthly();
					break;
				case "AH002":	//년도별
					table_monthly.Visible = false;
					table_yearly.Visible = true;
					SelectYearly();
					break;
				default:
					table_monthly.Visible = true;
					table_yearly.Visible = false;
					SelectMonthly();
					break;
			}
		}

		#endregion

		#region Page_PreRender

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//월간
		protected void gv_monthly_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				for (int i = 0; i < e.Row.Cells.Count; i++)
				{
					e.Row.Cells[i].Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
					e.Row.Cells[i].Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);
				}
			}
		}

		//연간
		protected void gv_yearly_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//일자
				e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();

				//셀바탕색				
				for (int i = 0; i < e.Row.Cells.Count; i++)
				{
					if (e.Row.Cells[i].Text.Length > 37)
					{
						string ls_day_of_week = e.Row.Cells[i].Text.Substring(27, 3);
						string ls_holiday_yn = e.Row.Cells[i].Text.Substring(30, 1);

						//주말처리
						if (ls_day_of_week == "Sat")
						{
							e.Row.Cells[i].Attributes.Add("class", "bg_bluesky");
						}
						else if (ls_day_of_week == "Sun")
						{
							e.Row.Cells[i].Attributes.Add("class", "bg_pink");
						}

						//휴일처리
						if (ls_holiday_yn == "1")
						{
							e.Row.Cells[i].Attributes.Add("class", "bg_pink");
						}
					}
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledMonthBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_baby_food_stuff", typeof(string), kw2);
			
			switch (rbl_layout.SelectedValue)
			{
				case "AH001":	//월별
					using (Bby2012r_NT _Bby2012r_NT = new Bby2012r_NT())
					{
						lo_ds.Merge(_Bby2012r_NT.GetMonthlyBabyFoodStuff(lo_dp));

						string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")(Monthly)";
						ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET), "Excel1"));
					}
					break;
				case "AH002":	//년도별
					using (Bby2012r_NT _Bby2012r_NT = new Bby2012r_NT())
					{
						lo_ds.Merge(_Bby2012r_NT.GetYearlyBabyFoodStuff(lo_dp));

						string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")(Yearly)";
						ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET), "Excel2"));
					}
					break;
			}
		}

		//레이아웃변경시
		protected void rbl_layout_SelectedIndexChanged(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}

		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}

		//이전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//다음년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		#endregion

	}
}
